package scct.hys.dao;

import scct.hys.model.Hall;
import scct.hys.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HallDao {
    public static List<Hall> getAllHalls(){
        List<Hall> list=new ArrayList<>();
        Connection conn= DBUtils.getConnection();
        String sql="select * from cm_hall";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            while (rs.next()){
                int id=rs.getInt("id");
                String movieid=rs.getString("movieid");
                String screentype=rs.getString("screentype");
                String screensize=rs.getString("screensize");
                String total=rs.getString("total");

                list.add(new Hall(id,movieid,screentype,screensize,total));
            }
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }
    public static boolean addHall(String movieid, String screentype, String screensize, String total){
        int result=0;
        Connection conn=DBUtils.getConnection();
        String insert_sql="insert into cm_hall values(null,?,?,?,?)";
        try {
            PreparedStatement pstmt=conn.prepareStatement(insert_sql);
            pstmt.setString(1,movieid);
            pstmt.setString(2,screentype);
            pstmt.setString(3,screensize);
            pstmt.setString(4,total);
            result=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return result>0;
    }
    public boolean updateHall(int id, String movieid, String screentype, String screensize, String total){
        System.out.println(movieid);
    int count=0;
    Connection conn=DBUtils.getConnection();
    String update_sql="update cm_hall set movieid=?,screentype=?,screensize=?,total=? where id=?";
    try {
        PreparedStatement pstmt=conn.prepareStatement(update_sql);
        pstmt.setString(1,movieid);
        pstmt.setString(2,screentype);
        pstmt.setString(3,screensize);
        pstmt.setString(4,total);
        pstmt.setInt(5,id);

        count=pstmt.executeUpdate();
    }catch (SQLException e){
        throw new RuntimeException(e);
    }
    return count>0;
    }
    public  Hall getHallById(int id){
        Hall hall=null;
        Connection conn=DBUtils.getConnection();
        String sql="select * from cm_hall where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs=pstmt.executeQuery();
        if (rs.next()){
            String movieid=rs.getString("movieid");
            String screentype=rs.getString("screentype");
            String screensize=rs.getString("screensize");
            String total=rs.getString("total");
            hall=new Hall(id,movieid,screentype,screensize,total);
        }
        DBUtils.closeConnection(conn);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        return hall;
    }
    public boolean delHall(int id){
        System.out.println(id);
        int num=0;
        Connection conn=DBUtils.getConnection();
        String del_sql="delete from cm_hall where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(del_sql);
            pstmt.setInt(1,id);
            num=pstmt.executeUpdate();

        }catch (SQLException e){
            e.printStackTrace();
        }
        return num>0;
    }

}
